from threading import Thread, Lock, Semaphore
from time import sleep
from threading import Condition

lock = Lock()
total = 0


class GetHtmlContent(Thread):

    def __init__(self, url, sem):
        super(GetHtmlContent, self).__init__()
        self.url = url
        self.sem = sem

    def run(self):
        sleep(2)
        print('get the url:[ {} ]\'s html content success!!'.format(self.url))
        self.sem.release()


class URLProducer(Thread):
    def __init__(self, sem):
        super(URLProducer, self).__init__()
        self.sem = sem

    def run(self):
        for i in range(20):
            self.sem.acquire()
            get_hmtl_content = GetHtmlContent("https://baidu.com/p/{}".format(str(i)), self.sem)
            get_hmtl_content.start()


if __name__ == '__main__':
    # define the semaphore 's size
    sem = Semaphore(3)
    url_producer = URLProducer(sem)
    url_producer.start()
